<?php

/**
 * RepairMsgtpl报修消息模版推送
 *
 * @author hjc19850430@gmail.com
 */
class RepairnewMsgtpl extends BaseMsgtpl
{
    /**
     * 自动派单发送消息
     * @param array $wechat 公众号信息
     * @param array $order 订单信息
     * @param array $repairman 维修工
     * @return
     */
    public function autoOperateSendMsg($order, $repairman, $accountId)
    {
       // error_log('RepairUserOrderController/sendMsg :---There has one error:---send one:---:' . var_export($order, true) . '---user id:---:' . var_export($repairman, true) . " \r\n", 3, '/wwwlogs/repairnew_user_order_error_new.log');

        $wechatUserInfo = D('Wechat/WechatUser')->getInfo(array('user_id' => $repairman), 'openid,subscribe,wechat_id');

       // error_log('RepairUserOrderController/sendMsg :---There has one error:---send one:---:' . var_export($wechatUserInfo, true) . '---account id:---:' . var_export($accountId, true) . " \r\n", 3, '/wwwlogs/repairnew_user_order_error_new.log');
        $url = C('site_url') . "/repairnew/repairnewReceive/index/repairId/" . $order['repair_id'] . "/order_id/" . $order['order_id'];
        if ($accountId == 2) {
            $template_id = 'D3IHmxcLckGTHiXw-LDlApX9VIWdOvcZgVqyunmESjo';
        } elseif ($accountId == 5) {
            $template_id = '4EeswD5oYCEQJi2u8AOzAQrflX5aCQVxFOcr592-fvY';
        } elseif ($accountId == 6) {
            $template_id = 'y2OZQJsNL3wXLOrbTuXzuX2x5IVT_ZDsLodN3Mpzyug';
        } elseif ($accountId == 4) {  //测试服
            $template_id = 'k203JXlZ8F8SCYUIR3uH_HBL8Uq2aAMbAIv2N1r46ec';
        } elseif ($accountId == 10) {  //铁道部
            $template_id = 'RUdkrbYCT4fM8eYiEzMGoFrdj7tM2g9YI_Px4xaxETU';
        } elseif ($accountId == 60) {
            $template_id = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs';
        } elseif ($accountId == 3) {
            $template_id = 'Xip28HCL4EbvNB6bItDLrIheQnOceLI87-ZDE5f4m0A';
        } elseif ($accountId == 17) {
            $template_id = 'nr3BnPPtM-TA11B537yEf9_CRIV9KXbL9wn7lDJxWFg';
        } elseif ($accountId == 22) {
            $template_id = 'F-qFB-UE6XsmlvpM5liaOwXOLmuaTUosK7A_pz7im9o';
        } elseif ($accountId == 65) {//紫元物业
            $template_id = 'xuLPonzO3SE36UfXU3kJI4HAXQ0Q80cHWR1Unv5xCHg';
        }

        //if($wechatUserInfo && $wechatUserInfo['subscribe']){
        if ($accountId == 22 || $accountId == 65) {
            $projectName = '';
            if ($order['project_id']) {
                $projectInfo = D('Repairnew/RepairnewProject')->getByProjectId($order['project_id']);
                $projectName = $projectInfo['project_name'];
            }
            $childProject = '';
            if ($order['project_child_id']) {
                $childInfo = D('Repairnew/RepairnewProject')->getByProjectId($order['project_child_id']);
                $childProject = ' ' . $childInfo['project_name'];
            }

            $projects = $projectName ? $projectName . ($childProject ? $childProject : '') : '-';
            $info = array(
                'first' => array("value" => '您有新的派单，请在您的维修广场查看！', 'color' => '#173177'),
                'keyword1' => array("value" => $order['real_name']),
                'keyword2' => array("value" => $order['content']),
                'keyword3' => array("value" => $projects),
                'keyword4' => array("value" => $order['community_name'] . $order['building_name'] . $order['room_name'] . $order['detail_address']),
                'keyword5' => array("value" => date("Y-m-d H:i:s"), "color" => "#173177"),
                'remark' => array("value" => "点击查看详情！", "color" => "#173177"),
            );
        } else {
            $info = array(
                'first' => array("value" => '您有新的派单，请在您的维修广场查看！', 'color' => '#173177'),
                'keyword1' => array("value" => $order['real_name']),
                'keyword2' => array("value" => date("Y-m-d H:i:s"), "color" => "#173177"),
                'keyword3' => array("value" => date('Y-m-d H:i:s', strtotime("+1 hour")), "color" => "#173177"),
                'remark' => array("value" => "点击查看详情！", "color" => "#173177"),
            );
        }

        $result = $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUserInfo['openid'], $template_id, $info, $url);

        return $result;
        //}

        return false;
    }

    /**
     * 方法参数可以自己修改，添加
     * @param array $userData 用户信息
     * @param array $orderInfo 订单信息
     */
    public function orderConfirmSendMsg($orderId, $accountId)
    {
        $mOrder = D('RepairnewOrder');
        $mUser = D('Base/User');
        $orderInfo = $mOrder->getByOrderId($orderId);
        $repairman = $mUser->getByPk($orderInfo['repairman_id']);
        $wechatUserInfo = D('Wechat/WechatUser')->getInfo(array('user_id' => $orderInfo['user_id']), 'openid,subscribe');
        $url = C('site_url') . '/index.php?g=repairnew&m=repairnewUserOrder&a=userOrderDetail&repairId=' . $orderInfo['repair_id'] . '&order_id=' . $orderInfo['order_id'];
        //if($wechatUserInfo['subscribe']) {
        if ($accountId == 4) {
            $array = array(
                'first' => array("value" => $orderInfo['real_name'] . '，您的维修申请已经通过。', 'color' => '#173177'),
                'keyword2' => array("value" => $repairman['real_name']),
                'keyword1' => array("value" => date("Y-m-d H:i:s"), "color" => "#173177"),
                'remark' => array("value" => "请点击查看！", "color" => "#173177"),
            );
        } elseif ($accountId == 65) {
            $projectName = '';
            if ($orderInfo['project_id']) {
                $projectInfo = D('Repairnew/RepairnewProject')->getByProjectId($orderInfo['project_id']);
                $projectName = $projectInfo['project_name'];
            }
            $childProject = '';
            if ($orderInfo['project_child_id']) {
                $childInfo = D('Repairnew/RepairnewProject')->getByProjectId($orderInfo['project_child_id']);
                $childProject = ' ' . $childInfo['project_name'];
            }

            $projects = $projectName ? $projectName . ($childProject ? $childProject : '') : '-';
            $array = array(
                'first' => array("value" => $orderInfo['real_name'] . '，您的维修申请已经通过。', 'color' => '#173177'),
                'keyword1' => array("value" => $orderInfo['real_name']),
                'keyword2' => array("value" => $orderInfo['content']),
                'keyword3' => array("value" => $projects),
                'keyword4' => array("value" => $orderInfo['community_name'] . $orderInfo['building_name'] . $orderInfo['room_name'] . $orderInfo['detail_address']),
                'keyword5' => array("value" => date("Y-m-d H:i:s"), "color" => "#173177"),
                'remark' => array("value" => "点击查看详情！", "color" => "#173177"),
            );
        } else {
            $array = array(
                'first' => array("value" => $orderInfo['real_name'] . '，您的维修申请已经通过。', 'color' => '#173177'),
                'keyword1' => array("value" => '订单号' . $orderInfo['order_sn']),
                'keyword2' => array("value" => date("Y-m-d H:i:s")),
                'keyword3' => array("value" => '123456'),
                'remark' => array("value" => "请点击查看！", "color" => "#173177"),
            );
        }


        if ($accountId == 2) {
            $template_id = 'ervBp_BlUtHqKsaIw0qjmFz2KXy9J1tWCF5TIhW042Q';
        } elseif ($accountId == 5) {
            $template_id = 'p_P3GalanUy8syyholudeTR3OB5hzEuEAz-w12D_lbQ';
        } elseif ($accountId == 6) {
            $template_id = 'DAxDdnfAhywT-lfRTsaSGOQwd96g5RQnXMTigQknWCA';
        } elseif ($accountId == 4) {
            $template_id = 'k203JXlZ8F8SCYUIR3uH_HBL8Uq2aAMbAIv2N1r46ec';
        } elseif ($accountId == 10) {
            $template_id = 'A4NBwXw1VQmYDOBzCImVstEnwLG2tvbkztK8vbJkc-I';
        } elseif ($accountId == 60) {
            $template_id = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs';
        } elseif ($accountId == 3) {
            $template_id = 'yI0d_ILucYN4r_4lfnqsvwUOPd94K7Se7rQkUScaEiU';
        } elseif ($accountId == 17) {
            $template_id = 'qV44STOD0tpw_b2JHWrV7TsBk3uYdjpuCIYSp5gJqb4';
        } elseif ($accountId == 22) {
            $template_id = 'WiguocN3goznC5lVyB4yw100e_wrEvISvoFGHStUhN0';
        } elseif ($accountId == 65) {
            $template_id = 'xuLPonzO3SE36UfXU3kJI4HAXQ0Q80cHWR1Unv5xCHg';
        }

        $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUserInfo['openid'], $template_id, $array, $url);
        //}

    }

    /**
     * 订单完成处理
     * @param array $userData
     * @param array $orderInfo
     */
    public function orderFinishSendMsg($orderInfo, $accountId)
    {
        $mUser = D('Base/User');
        $repairman = $mUser->getFieldByUserId($orderInfo['repairman_id'], 'real_name');
        $wechatUserInfo = D('Wechat/WechatUser')->getInfo(array('user_id' => $orderInfo['user_id']), 'openid,subscribe');
        //$url = C('site_url')."/base/repair/orderDetail/repairId/".$orderInfo['repair_id']."/order_id/".$orderInfo['order_id'];
        $url = C('site_url') . '/index.php?g=repairnew&m=repairnewUserOrder&a=userOrderDetail&repairId=' . $orderInfo['repair_id'] . '&order_id=' . $orderInfo['order_id'];
        //if($wechatUserInfo['subscribe']) {
        if ($orderInfo['pay_type'] == 1 && $orderInfo['pay_time'] == 0) {
            $array = array(
                'first' => array("value" => $orderInfo['real_name'] . '，您的维修已经完成,维修金额' . $orderInfo['order_amount'] . '元，请尽快完成支付！', 'color' => '#173177'),
                'keyword2' => array("value" => $repairman),
                'keyword1' => array("value" => date("Y-m-d H:i:s"), "color" => "#173177"),
                'remark' => array("value" => "请点击查看！", "color" => "#173177"),
            );
        } elseif ($accountId == 65) {
            $projectName = '';
            if ($orderInfo['project_id']) {
                $projectInfo = D('Repairnew/RepairnewProject')->getByProjectId($orderInfo['project_id']);
                $projectName = $projectInfo['project_name'];
            }
            $childProject = '';
            if ($orderInfo['project_child_id']) {
                $childInfo = D('Repairnew/RepairnewProject')->getByProjectId($orderInfo['project_child_id']);
                $childProject = ' ' . $childInfo['project_name'];
            }

            $projects = $projectName ? $projectName . ($childProject ? $childProject : '') : '-';
            $array = array(
                'first' => array("value" => $orderInfo['real_name'] . '，您的维修已经完成。', 'color' => '#173177'),
                'keyword1' => array("value" => $orderInfo['real_name']),
                'keyword2' => array("value" => $orderInfo['content']),
                'keyword3' => array("value" => $projects),
                'keyword4' => array("value" => $orderInfo['community_name'] . $orderInfo['building_name'] . $orderInfo['room_name'] . $orderInfo['detail_address']),
                'keyword5' => array("value" => date("Y-m-d H:i:s"), "color" => "#173177"),
                'remark' => array("value" => "点击查看详情！", "color" => "#173177"),
            );
        } else {
            $array = array(
                'first' => array("value" => $orderInfo['real_name'] . '，您的维修已经完成。', 'color' => '#173177'),
                'keyword2' => array("value" => $repairman),
                'keyword1' => array("value" => date("Y-m-d H:i:s"), "color" => "#173177"),
                'remark' => array("value" => "请点击查看！", "color" => "#173177"),
            );
        }

        if ($accountId == 2) {
            $template_id = 'ervBp_BlUtHqKsaIw0qjmFz2KXy9J1tWCF5TIhW042Q';
        } elseif ($accountId == 5) {
            $template_id = 'p_P3GalanUy8syyholudeTR3OB5hzEuEAz-w12D_lbQ';
        } elseif ($accountId == 6) {
            $template_id = 'DAxDdnfAhywT-lfRTsaSGOQwd96g5RQnXMTigQknWCA';
        } elseif ($accountId == 4) {
            $template_id = 'k203JXlZ8F8SCYUIR3uH_HBL8Uq2aAMbAIv2N1r46ec';
        } elseif ($accountId == 10) {
            $template_id = 'A4NBwXw1VQmYDOBzCImVstEnwLG2tvbkztK8vbJkc-I';
        } elseif ($accountId == 60) {
            $template_id = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs';
        } elseif ($accountId == 3) {
            $template_id = 'yI0d_ILucYN4r_4lfnqsvwUOPd94K7Se7rQkUScaEiU';
        } elseif ($this->wechatInfo['account_id'] == 17) {
            $template_id = 'qV44STOD0tpw_b2JHWrV7TsBk3uYdjpuCIYSp5gJqb4';
        } elseif ($accountId == 22) {
            $template_id = 'WiguocN3goznC5lVyB4yw100e_wrEvISvoFGHStUhN0';
        } elseif ($accountId == 65) {
            $template_id = 'xuLPonzO3SE36UfXU3kJI4HAXQ0Q80cHWR1Unv5xCHg';
        }
        $result = $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUserInfo['openid'], $template_id, $array, $url);
        return $result;
        //}
        return false;
    }

    /**
     * 获得微信对象
     * @param int $wechatId 公众号对应的主键值
     */
    protected function getWechatObj($wechatId)
    {
        vendor('Wechat.WechatApiToRepairnew');
        $weixin = new WechatApiToRepairnew($wechatId);

        return $weixin;
    }

    /**
     * 维修工无法维修提交返修,发消息给用户
     * @param array $userData 用户信息
     * @param array $orderInfo 订单信息
     */
    /*public function backSendMessage($order,$accountId)
    {
        $mOrder = D('RepairOrder');
        $mUser = D('User');
        $orderInfo = $mOrder->getByPk($order['order_id']);
        $repairman = $mUser->getByPk($orderInfo['repairman_id']);
        $wechatUserInfo = D('Wechat/WechatUser')->getInfo(array('user_id'=>$orderInfo['user_id']), 'openid,subscribe');
        $url = C('site_url').'/index.php?g=base&m=repair&a=orderDetail&repairId='.$orderInfo['repair_id'].'&order_id='.$orderInfo['order_id'];
        if($wechatUserInfo['subscribe']) {

                $array = array(
                        'first' => array("value" => $orderInfo['real_name'].'，您的维修单已提交返修,请确认并重新下单。','color' => '#FF0000'),
                        'keyword2' => array("value" => $repairman['real_name'] ),
                        'keyword1' => array("value" => date("Y-m-d H:i:s"),"color" => "#173177"),
                        'remark' => array("value" => "请点击查看！","color" => "#173177"),
                );



            if($accountId == 2){
                $template_id = 'ervBp_BlUtHqKsaIw0qjmFz2KXy9J1tWCF5TIhW042Q';
            }elseif($accountId==5){
                $template_id = 'p_P3GalanUy8syyholudeTR3OB5hzEuEAz-w12D_lbQ';
            }elseif($accountId==6){
                $template_id = 'DAxDdnfAhywT-lfRTsaSGOQwd96g5RQnXMTigQknWCA';
            }elseif($accountId == 4){
                $template_id = 'k203JXlZ8F8SCYUIR3uH_HBL8Uq2aAMbAIv2N1r46ec';
            }elseif($accountId == 10){
                $template_id = 'A4NBwXw1VQmYDOBzCImVstEnwLG2tvbkztK8vbJkc-I';
            }elseif($accountId == 28){
                $template_id = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs';
            }elseif($accountId==3){
                $template_id = 'yI0d_ILucYN4r_4lfnqsvwUOPd94K7Se7rQkUScaEiU';
            }elseif($this->wechatInfo['account_id'] == 17){
                $template_id = 'qV44STOD0tpw_b2JHWrV7TsBk3uYdjpuCIYSp5gJqb4';
            }
            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUserInfo['openid'], $template_id, $array, $url);
        }

    }*/

// 	/**
// 	 * 创建订单完成后发送短信
// 	 * @param array $userData	用户信息
// 	 * @param array $orderInfo	订单信息
// 	 */
// 	public function createOrderSendMsg($userData, $orderInfo)
// 	{
// 		//todo...
// 	}

// 	/**
// 	 * 订单评论发送消息
// 	 * @param array $userData	用户信息
// 	 * @param array $orderInfo 订单信息
// 	 */
// 	public function orderCommentSendMsg($userData, $orderInfo)
// 	{
// 		//todo...
// 	}
}
